package com.tengju.data.domain.model.clickhouse;

import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import com.tengju.data.domain.shared.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDateTime;

/**
 * 人群,时间范围 视角
 */
@Getter
@AllArgsConstructor
public enum CrmRankCrowdDatePerspective {

    KA_CURRENT_MONTH(CrmRankCrowdPerspective.KA,MonthQuarterEnum.CURRENT_MONTH,"month_star_gen_sec_rank","month_dealer_members","month_star_gen_sec_gmv"),
    KA_LAST_MONTH(CrmRankCrowdPerspective.KA,MonthQuarterEnum.LAST_MONTH,"month_star_gen_sec_rank","month_dealer_members","month_star_gen_sec_gmv"),
    KA_CURRENT_QUARTER(CrmRankCrowdPerspective.KA,MonthQuarterEnum.CURRENT_QUARTER,"quarter_star_gen_sec_rank","quarter_dealer_members","quarter_star_gen_sec_gmv"),

    EC_CURRENT_MONTH(CrmRankCrowdPerspective.EC,MonthQuarterEnum.CURRENT_MONTH,"month_star_ec_rank","month_dealer_members","month_star_ec_gmv"),
    EC_LAST_MONTH(CrmRankCrowdPerspective.EC,MonthQuarterEnum.LAST_MONTH,"month_star_ec_rank","month_dealer_members","month_star_ec_gmv"),
    EC_CURRENT_QUARTER(CrmRankCrowdPerspective.EC,MonthQuarterEnum.CURRENT_QUARTER,"quarter_star_ec_rank","quarter_dealer_members","quarter_star_ec_gmv"),

    CITY_STAR_CURRENT_MONTH(CrmRankCrowdPerspective.CITY_STAR,MonthQuarterEnum.CURRENT_MONTH,"month_star_city_rank","month_dealer_members","month_star_city_gmv"),
    CITY_STAR_LAST_MONTH(CrmRankCrowdPerspective.CITY_STAR,MonthQuarterEnum.LAST_MONTH,"month_star_city_rank","month_dealer_members","month_star_city_gmv"),
    CITY_STAR_CURRENT_QUARTER(CrmRankCrowdPerspective.CITY_STAR,MonthQuarterEnum.CURRENT_QUARTER,"quarter_star_city_rank","quarter_dealer_members","quarter_star_city_gmv"),

    DEALER_CURRENT_MONTH(CrmRankCrowdPerspective.DEALER,MonthQuarterEnum.CURRENT_MONTH,"month_dealer_rank","month_dealer_members","month_dealer_gmv"),
    DEALER_LAST_MONTH(CrmRankCrowdPerspective.DEALER,MonthQuarterEnum.LAST_MONTH,"month_dealer_rank","month_dealer_members","month_dealer_gmv"),
    DEALER_CURRENT_QUARTER(CrmRankCrowdPerspective.DEALER,MonthQuarterEnum.CURRENT_QUARTER,"quarter_dealer_rank","quarter_dealer_members","quarter_dealer_gmv"),
    ;

    private final CrmRankCrowdPerspective crmRankCrowdPerspective;
    private final MonthQuarterEnum monthQuarterEnum;
    private final String sqlRankField;
    private final String sqlDealerMemberField;
    private final String sqlGmvField;


    public static CrmRankCrowdDatePerspective parseByRankAndDateRange(CrmRankCrowdPerspective crmRankCrowdPerspective, MonthQuarterEnum monthQuarterEnum) {

        if (crmRankCrowdPerspective == null || monthQuarterEnum == null) {
            return null;
        }

        for (CrmRankCrowdDatePerspective type : values()) {
            if (type.crmRankCrowdPerspective == crmRankCrowdPerspective &&
                    type.monthQuarterEnum == monthQuarterEnum) {
                return type;
            }
        }
        return null;
    }


    public Integer getMonth() {
        LocalDateTime now = LocalDateTime.now();
        switch (this.monthQuarterEnum) {
            case CURRENT_MONTH:
                return DateUtil.formatMonthInt(now);
            case LAST_MONTH:
                return DateUtil.formatLastMonthInt(now);
            default:
                return null;
        }
    }

    public Integer getCompareMonth() {
        LocalDateTime now = LocalDateTime.now();
        switch (this.monthQuarterEnum) {
            case CURRENT_MONTH:
                return DateUtil.formatLastMonthInt(now);
            default:
                return null;
        }
    }

    public Integer getQuarter() {
        LocalDateTime now = LocalDateTime.now();
        switch (this.monthQuarterEnum) {
            case CURRENT_QUARTER:
                return DateUtil.formatCurrentQuarterInt(now);
            default:
                return null;
        }
    }

    public Integer getCompareQuarter() {
        LocalDateTime now = LocalDateTime.now();
        switch (this.monthQuarterEnum) {
            case CURRENT_QUARTER:
                return DateUtil.formatLastQuarterInt(now);
            default:
                return null;
        }
    }

}
